#!/usr/bin/env python
# USP and USPconfig Template plugin
##Description:A simple plugin template file
try:
    import pygtk
    pygtk.require( "2.0" )
except:
    pass

#import of modules needed for functions
# e.g. math for log, sin, tan, etc.
# e.g. pango for style attributes
import gtk
import gtk.glade
import sys
import os
import gmenu
import gobject
import gconf
import pango
import datetime
import math

from easyfiles import *
from easybuttons import *
from execute import *
from easygconf import *

class pluginclass:
	TARGET_TYPE_TEXT = 80
	toButton = [ ( "text/uri-list", 0, TARGET_TYPE_TEXT ) ]
	"""This is the main class for the plugin"""
	"""It MUST be named pluginclass"""

	def __init__(self, USPWin):

		self.USPWin = USPWin

		#The Glade file for the plugin
		self.gladefile = os.path.join(os.path.dirname(__file__), "template.glade")

		#Read GLADE file
		self.wTree = gtk.glade.XML(self.gladefile,"window1")

		#These properties are NECESSARY to maintain consistency
		#throughout USP

		#Set 'window' property for the plugin (Must be the root widget)
		self.window = self.wTree.get_widget("window1")

		#Set 'heading' property for plugin
		self.heading = "Template"

		#This should be the first item added to the window in glade
		self.content_holder = self.wTree.get_widget("eventbox1")

		# Items in the Glade file to get custom colors, store in an array
		self.itemstocolor = [self.wTree.get_widget('vbox1')]

		# GConf stuff notify_add will call RegenPlugin if one of the listed gconf entries is changed.
		self.gconf_dir = '/apps/usp/plugins/template'
		self.client = gconf.client_get_default()
		self.client.add_dir('/apps/usp/plugins/template', gconf.CLIENT_PRELOAD_NONE)
		self.client.notify_add('/apps/usp/plugins/template/height',self.RegenPlugin)
		self.client.notify_add('/apps/usp/plugins/template/width',self.RegenPlugin)
		self.client.notify_add('/apps/usp/plugins/template/font_size',self.RegenPlugin)
		self.client.notify_add('/apps/usp/plugins/template/entry',self.RegenPlugin)
		self.client.notify_add('/apps/usp/plugins/template/sticky',self.RegenPlugin)
		self.client.notify_add('/apps/usp/use_custom_color',self.RegenPlugin)
		self.client.notify_add('/apps/usp/custom_color',self.RegenPlugin)

		self.RegenPlugin()
		self.wTree.get_widget("window1").set_size_request(self.height,self.width)
		#Connect event handlers for Glade File Items
		dic = { "on_window1_destroy" : gtk.main_quit,
				"on_TemplateEntry1_changed" : self.DoSomething}
		self.wTree.signal_autoconnect(dic)

		# Do Something with the Plugin. You can change the name of this function.
		self.DoPluginStuff()
		# Or if it needs to be repeated at intervals to update info that is displayed.
		# Uncomment next line and alter the function to what you have called you're function.
		# gobject.timeout.add(1000,self.DoPluginStuff)
		# This can add as  much as 5% to the cpu load if used for disk intense functions
		# Only use if the info displayed needs to be changed frequently.
		# 1000 = 1 Second, 60000 = 60 Seconds, etc.
	# End of Main Plugin Class

	### Functions Used within pluginclass
	# Main Function called at the beginning of USP or every timeout.add event if set.
	def DoPluginStuff(self, *args, **kargs):
		self.wTree.get_widget('TemplateEntry1').set_text(self.entry)
		# If this function is a timeout.add function it must return True or it will cease repeating.
		return True

	# Function called to Set Colors of Items
	def SetPaneColors( self, items ):
		if self.usecustomcolor:
			for item in items:
				item.modify_bg( gtk.STATE_NORMAL, gtk.gdk.color_parse( self.customcolor ) )

	# Function called to Set Font Size of Items
	def SetFontSize( self, items, itemfontsize):
		for item in items:
			PStyle = pango.AttrList()
			attr = pango.AttrSize(itemfontsize*1000,0,-1)
			PStyle.insert(attr)
			self.wTree.get_widget(item).set_attributes(PStyle)

	# Function called whenever a notify_add event occurs
	def RegenPlugin(self, *args, **kargs):
		self.GetGconfEntries()
		self.SetPaneColors(self.itemstocolor)
		self.SetFontSize(['TemplateEntryLabel1'], self.fontsize)

	# Function for Getting Initial Gconf Entries
	# SetGConf() writes the final parameter as a default value if one is not already there.
	def GetGconfEntries(self):
		# Get USP required GConf entries------------------#
		self.client = gconf.client_get_default()
		self.height = SetGconf(self.client,'int','/apps/usp/plugins/template/height',200)
		self.width = SetGconf(self.client,'int','/apps/usp/plugins/template/width',250)
		# Allow plugin to be minimized to the left plugin pane
		self.minimized = SetGconf( self.client, "bool", "/apps/usp/plugins/template/minimized", False )
		self.sticky = SetGconf( self.client, "bool", "/apps/usp/plugins/template/sticky", False )
		# Icon for the plugin
		self.icon = SetGconf( self.client, "string", '/apps/usp/plugins/template/icon', "computer.png" )
		# End of USP required GConf entries---------------#

		# Get the Main USP custom color options so plugin is consistent
		self.usecustomcolor = SetGconf(self.client,'bool','/apps/usp/use_custom_color',False)
		self.customcolor = SetGconfColor(self.client,'/apps/usp/custom_color',"#d4d4d4")

		# Get Plugin specific GConf entries
		self.fontsize = SetGconf(self.client,'int','/apps/usp/plugins/template/font_size',14)
		self.entry = SetGconf(self.client,'string','/apps/usp/plugins/template/entry','Some Text')

	# This Function is Necessary for USP to Track the Plugin State
	def SetHidden( self, state ):
		if state == True:
			WriteGconf( self.client, "bool", "/apps/usp/plugins/template/minimized", True )
		else:
			WriteGconf( self.client, "bool", "/apps/usp/plugins/template/minimized", False )

	# A sample function for when an event occurs.
	# In this case when the entry box content is changed the label changes.
	def DoSomething(self, *args, **kargs):
		EntryBoxText=self.wTree.get_widget('TemplateEntry1').get_text()
		if EntryBoxText==self.entry:
			self.wTree.get_widget('TemplateEntryLabel1').set_text(self.entry)
		else:
			self.wTree.get_widget('TemplateEntryLabel1').set_text(EntryBoxText)

	# Other Event Functions for the plugin can go here.
	# -------------------------------------------#

#---------------------------------------------------------------------------------------#
# USPconfig Section Below
#---------------------------------------------------------------------------------------#
# This is the Config Class it must be called "cfgpluginclass' and the gtk.Window must be called 'config'
class cfgpluginclass:
	def __init__(self):
		# The Gladefile for the plugins USPconfig Tab
		self.gladefile = os.path.join(os.path.dirname(__file__), "template.glade")
		# Read GLADE file
		self.wTree = gtk.glade.XML(self.gladefile,"config")
		# Set 'window' property for the plugin (Must be the root widget)
		self.window = self.wTree.get_widget("config")
		# Content Place Holder must be eventbox2
		self.content_holder = self.wTree.get_widget("eventbox2")
		# Set Heading, this will be used for the tab label in USPconfig
		self.heading = "Template"
		# GConf Stuff - This just makes sure a gconf path is there.
		self.gconf_dir = '/apps/usp/plugins/template'
		self.client = gconf.client_get_default()
		self.client.add_dir('/apps/usp/plugins/template', gconf.CLIENT_PRELOAD_NONE)
		# Setup the Functions of the Glade files spin controls or entry boxes for Value Changes
		# Tip: For spinbutton controls use the "_value_changed" event not the "_changed" event.
		dic = { "on_window2_destroy" : gtk.main_quit,
				"on_TemplateHeightSpin_changed" : self.height_spin,
				"on_TemplateWidthSpin_changed" : self.width_spin,
				"on_TemplateFontSizeSpin_changed" : self.font_spin,
				"on_TemplateEntry1_changed" : self.entry1_changed,
				"on_TemplateIconEntry_changed" : self.icon_changed,
				"on_StickyChkBtn_toggled" : self.sticky}
		self.wTree.signal_autoconnect(dic)
		# Read Values from GConf for USPconfig Tab and Set Default Values in GConf if they don't exist.
		self.wTree.get_widget("StickyChkBtn").set_active(SetGconf(self.client,'bool','/apps/usp/plugins/template/sticky',False))
		self.wTree.get_widget("TemplateIconEntry").set_text(SetGconf( self.client, "string", '/apps/usp/plugins/template/icon', "computer.png"))
		self.wTree.get_widget("TemplateEntry1").set_text(SetGconf(self.client,'string','/apps/usp/plugins/template/entry','Some Text'))
		self.wTree.get_widget("TemplateHeightSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/template/height',200))
		self.wTree.get_widget("TemplateWidthSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/template/width',250))
		self.wTree.get_widget("TemplateFontSizeSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/template/font_size',14))

	# Functions for Changing Values when items checked or altered.
	# sticky is a CheckButton / True or False example.
	def sticky(self, *args, **kargs):
		if self.wTree.get_widget("StickyChkBtn").get_active() == True:
			self.client.set_bool('/apps/usp/plugins/template/sticky',True)
		else:
			self.client.set_bool('/apps/usp/plugins/template/sticky',False)
	# CheckButton to GConf Logical MUST be done this way not as a simple assignment.
	# E.G. NOT Like This:
	# self.client.set_bool('/apps/usp/plugins/template/sticky',self.wTree.get_widget("StickyChkBtn").get_active())

	# icon_changed and entry1_changed are TextEntry / string examples.
	def icon_changed(self, *args, **kargs):
		self.client.set_string('/apps/usp/plugins/template/icon',self.wTree.get_widget("TemplateIconEntry").get_text())

	def entry1_changed(self, *args, **kargs):
		self.client.set_string('/apps/usp/plugins/template/entry',self.wTree.get_widget("TemplateEntry1").get_text())

	# height_spin, width_spin and font_spin are SpinButton / integer examples.
	# int() function MUST be used to convert SpinButton value from float
	def height_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/template/height',int(self.wTree.get_widget("TemplateHeightSpin").get_value()))

	def width_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/template/width',int(self.wTree.get_widget("TemplateWidthSpin").get_value()))

	def font_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/template/font_size',int(self.wTree.get_widget("TemplateFontSizeSpin").get_value()))
